package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.dto.RoomListDTO;
import com.demo.pojo.entity.Room;
import com.demo.service.RoomService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Author：潘建冰
 * Package：com.demo.controller
 * Project：demo
 * name：RoomController
 * Date：2025/1/27
 */

@RestController
@RequestMapping("/classroom")
@Validated
public class RoomController {

    private final RoomService roomService;

    public RoomController(RoomService roomService) {
        this.roomService = roomService;
    }

    /**
     * 根据id查询教室数据
     * @param id 教室id
     * @return Result
     */
    @GetMapping("/select")
    public Result<Room> select(@RequestParam Integer id){
        return Result.success(roomService.selectById(id));
    }

    /**
     * 保存教室数据
     * @param room 教室数据
     * @return  Result
     */
    @PostMapping("/save")
    public Result<Void> save(@Valid @RequestBody Room room){
        roomService.saveRoom(room);
        return Result.success();
    }

    /**
     * 修改教室数据
     * @param room 教室数据
     * @return  Result
     */
    @PutMapping("/update")
    public Result<Void> update(@Valid @RequestBody Room room){
        roomService.updateRoom(room);
        return Result.success();
    }

    /**
     * 删除教室数据
     * @param id 教室id
     * @return  Result
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam Integer id){
        roomService.removeById(id);
        return Result.success();
    }

    /**
     * 分页查询教室数据
     * @param pageNum 页码
     * @param pageSize 每页数据量
     * @param roName 教室名称
     * @param roCapacity 教室容量
     * @param roCampusId 校区ID
     * @return  Result
     */
    @GetMapping("/listPage")
    public Result<Map<String, Object>> listPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(required = false) String roName,
                                @RequestParam(required = false) Integer roCapacity,
                                @RequestParam(required = false) Long roCampusId){
        //封装DTO对象进行传递
        RoomListDTO roomListDTO = new RoomListDTO();
        roomListDTO.setRoName(roName);
        roomListDTO.setRoCapacity(roCapacity);
        roomListDTO.setRoCampusId(roCampusId);
        roomListDTO.setPageNum(pageNum);
        roomListDTO.setPageSize(pageSize);
        return Result.success(roomService.listPage(roomListDTO));
    }

    /**
     * 获取所有教室列表（不分页）
     * @return Result
     */
    @GetMapping("/all")
    public Result<List<RoomListDTO>> getAllClassrooms(){
        return Result.success(roomService.getRoomList());
    }

}



